//
//  TDL_SelectDateView.m
//  TDL_CYMSManager
//
//  Created by zhangkunjiu on 16/7/25.
//  Copyright © 2016年 TDL. All rights reserved.
//

#import "TDL_SelectDateView.h"
#import "Masonry.h"
#import "TDL_PickerDate.h"


#define kFONT_SIZE 15
#define kTEXTFIELD_HEIGHT 30
#define kTOP_SPACE 5

@interface TDL_SelectDateView ()<UITextFieldDelegate,TDL_PickerDateDelegate>

@property (nonatomic, strong) UITextField *startTextfield;

@property (nonatomic, strong) UITextField *endTextfield;

@end

@implementation TDL_SelectDateView
- (instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = RGBCOLOR(238, 238, 238);
        [self addSubview:self.startTextfield];
        [self addSubview:self.endTextfield];
    }
    return self;
}

- (void)show{
//    CGFloat height = 30;
//    CGFloat l_width = 50;
//    CGFloat space= 5;
    CGFloat t_width = [UIScreen mainScreen].bounds.size.width / 2 - 50;
//    // start
//    UILabel *startLabel = [[UILabel alloc] init];
//    startLabel.font = [UIFont systemFontOfSize:kFONT_SIZE];
//    startLabel.text = @"开始时间";
//    startLabel.textColor = [UIColor blackColor];
//    startLabel.textAlignment  =  NSTextAlignmentCenter;
//    [self addSubview:startLabel];
//    [startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self).offset(space);
//        make.top.equalTo(self);
//        make.height.equalTo(@(height));
//        make.width.equalTo(@(l_width));
//    }];
    NSDate * date = [NSDate date];
    NSDateFormatter * startDateFormatter = [[NSDateFormatter alloc] init];
    [startDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    //一周的秒数
    NSTimeInterval time = 1 * 24 * 60 * 60;
    NSDate *lastWeek = [date dateByAddingTimeInterval:-time];
    NSString *startDate =  [startDateFormatter stringFromDate:lastWeek];
    self.startTextfield.text = startDate;
    [self.startTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(10);
        make.top.equalTo(self).offset(7);
        make.height.equalTo(self).offset(-15);
        make.width.equalTo(@(t_width));
    }];
    UITapGestureRecognizer *startTextfieldTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(startTextfieldTapAction:)];
    [self.startTextfield addGestureRecognizer:startTextfieldTap];
    
//    // end
    UILabel *endLabel = [[UILabel alloc] init];
    endLabel.text = @"---";
    endLabel.textColor = [UIColor blackColor];
    endLabel.font = [UIFont systemFontOfSize:kFONT_SIZE];
    endLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:endLabel];
    [endLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.startTextfield.mas_right).offset(0);
        make.top.equalTo(self);
        make.width.equalTo(@(25));
        make.height.equalTo(self);
    }];
    
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *currentDateStr = [dateFormatter stringFromDate:currentDate];
    self.endTextfield.text = currentDateStr;
    [self.endTextfield  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(endLabel.mas_right).offset(0);
        make.top.equalTo(self.startTextfield);
        make.height.equalTo(self).offset(-15);
        make.width.equalTo(self.startTextfield);
    }];
    UITapGestureRecognizer *endTextfieldTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(endTextfieldTapAction:)];
    [self.endTextfield addGestureRecognizer:endTextfieldTap];
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.backgroundColor = RGBCOLOR(107, 184, 250);
    [searchBtn setTitle:@"查询" forState:UIControlStateNormal];
    [searchBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(handleSearchAction:) forControlEvents:UIControlEventTouchUpInside];
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:kFONT_SIZE];
    searchBtn.layer.masksToBounds = YES;
    searchBtn.layer.cornerRadius = 5.0;
    [self addSubview:searchBtn];
    [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-5);
        make.left.equalTo(self.endTextfield.mas_right).offset(10);
        make.top.equalTo(self.endTextfield);
        make.height.equalTo(self.startTextfield);
    }];
    
}
-(void)startTextfieldTapAction:(UITapGestureRecognizer *)tap{
    TDL_PickerDate *picker = [[TDL_PickerDate alloc] init];
    picker.tag = 190;
    picker.delegate = self;
    [picker show];
    
}
-(void)endTextfieldTapAction:(UITapGestureRecognizer *)tap{
    TDL_PickerDate *picker = [[TDL_PickerDate alloc] init];
    picker.tag = 191;
    picker.delegate = self;
    [picker show];
    
}
- (void)dismiss{
    [self removeFromSuperview];
}

- (NSMutableArray *)gainStartDateAndEndDate{
    NSMutableArray *arr = [NSMutableArray array];
    [arr addObject:self.startTextfield.text];
    [arr addObject:self.endTextfield.text];
    return arr;
}
// 搜索
- (void)handleSearchAction:(UIButton *)btn{
        if ([self.delegate respondsToSelector:@selector(doTheRefreshWithStart:end:)]) {
            [self.delegate doTheRefreshWithStart:self.startTextfield.text end:self.endTextfield.text];
        }
}

#pragma mark -- UITextfieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    TDL_PickerDate *picker = [[TDL_PickerDate alloc] init];
    if (textField == self.startTextfield) {
        picker.tag = 190;
    }
    if (textField == self.endTextfield){
        picker.tag = 191;
    }
    picker.delegate = self;
    [picker show];
}

#pragma maek --  TDL_PickerDateDelegate
- (void)pickerDate:(TDL_PickerDate *)pickerDate year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day
{
    NSString *text = [NSString stringWithFormat:@"%ld-%02ld-%02ld", (long)year, (long)month, (long)day];
    if (pickerDate.tag == 190) {
        self.startTextfield.text = text;
    }else if (pickerDate.tag == 191){
        self.endTextfield.text = text;
    }
}

#pragma mark -- 懒加载 --
- (UITextField *)endTextfield{
    if (!_endTextfield) {
        _endTextfield = [[UITextField alloc] init];
        _endTextfield.delegate = self;
        _endTextfield.font = [UIFont systemFontOfSize:kFONT_SIZE];
        _endTextfield.textColor = RGBCOLOR(180, 180, 180);
//        _endTextfield.borderStyle = UITextBorderStyleNone;
        _endTextfield.textAlignment = NSTextAlignmentCenter;
        _endTextfield.layer.borderWidth = 1;
        _endTextfield.layer.borderColor = RGBCOLOR(200, 200, 200).CGColor;
        _endTextfield.layer.cornerRadius = 5;
        _endTextfield.clipsToBounds = YES;
        _endTextfield.backgroundColor = [UIColor whiteColor];
    }
    return _endTextfield;
}
- (UITextField *)startTextfield{
    if (!_startTextfield) {
        _startTextfield = [[UITextField alloc] init];
        _startTextfield.delegate = self;
        _startTextfield.font = [UIFont systemFontOfSize:kFONT_SIZE];
        _startTextfield.textColor = RGBCOLOR(180, 180, 180);
//        _startTextfield.borderStyle = UITextBorderStyleNone;
        _startTextfield.textAlignment = NSTextAlignmentCenter;
        _startTextfield.layer.borderWidth = 1;
        _startTextfield.layer.borderColor = RGBCOLOR(200, 200, 200).CGColor;
        _startTextfield.layer.cornerRadius = 5;
        _startTextfield.clipsToBounds = YES;
        _startTextfield.backgroundColor = [UIColor whiteColor];
    }
    return _startTextfield;
}


@end
